/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.abstractcanvas.awt;

import java.awt.Color;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLColor 
{
	/**
	 * Convert an HTML color description (either as #AABBCC or rgb(r,g,b) or rgb(r,g,b,a)) into
	 * a java.awt.Color object
	 * @param col a string containing an HTML color description
	 * @return a java.awt.Color object, or null if the string cannot be decoded
	 */
	static public java.awt.Color getColor(String col)
	{
		col = col.trim();
		if (col.startsWith("#"))
		{
			if (col.length() == 7)
				return hexacolor6(col.substring(1));
			else if (col.length() == 4)
				return hexacolor3(col.substring(1));
		}
		else if (col.startsWith("rgb(") || col.startsWith("rgba("))
		{
			return rgbcolor(col);
		}

		return null;
	}

	/**
	 * Convert a string of the form "rgb(0,0,0)" or "rgba(0,0,0,0)" into a java.awt.Color object.
	 * 
	 * @param col the string
	 * @return the corresponding color
	 */
	private static Color rgbcolor(String col) 
	{
		try {
			String pstr = "rgb[a]?[ \t]*[(][ \t]*([0-9A-Fa-f]+)[ \t]*,[ \t]*([0-9A-Fa-f]+)[ \t]*,[ \t]*([0-9A-Fa-f]+)[ \t]*(,[ \t]*([0-9\\.]+)[ \t]*)?[)]"; // \\((\\d),(\\d),(\\d)\\)";
			Pattern p = Pattern.compile(pstr);
			Matcher m = p.matcher(col);

			System.out.println("res:" + m.matches());

			System.out.println("res:" + m);
			String s1 = m.group(1); // red
			String s2 = m.group(2); // green
			String s3 = m.group(3); // blue
			String s4 = m.group(5); // alpha


			if (s4 != null)
				return new Color(Integer.parseInt(s1), Integer.parseInt(s2), Integer.parseInt(s3), (int)(Double.parseDouble(s4)*255));
			else
				return new Color(Integer.parseInt(s1), Integer.parseInt(s2), Integer.parseInt(s3));
		} catch (Exception exc)
		{
			return null;
		}
	}

	/**
	 * Convert an hexadecimal 6-char html color code into a java.awt.Color object
	 * @param str the string, of the form "FF00FF"
	 * @return a Color object
	 */
	private static Color hexacolor6(String str) 
	{
		try {
			int r = getHVal(str.charAt(0), str.charAt(1));
			int g = getHVal(str.charAt(2), str.charAt(3));
			int b = getHVal(str.charAt(4), str.charAt(5));
			return new java.awt.Color(r,g,b);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Convert an hexadecimal 3-char html color code into a java.awt.Color object
	 * @param str the string, of the form "F0F"
	 * @return a Color object
	 */
	private static Color hexacolor3(String str) 
	{
		try {
			int r = getHVal(str.charAt(0), str.charAt(0));
			int g = getHVal(str.charAt(1), str.charAt(1));
			int b = getHVal(str.charAt(2), str.charAt(2));

			return new java.awt.Color(r,g,b);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	
	static private int getHVal(char c1, char c2)
	{
		int i = Integer.valueOf(""+c1+c2, 16);

		return i; // ((h1 <<8)&0xFF) | (h2&0xF);
	}	

	static public void main(String[]args)
	{
		System.out.println( HTMLColor.getColor("#09F"));
		System.out.println( HTMLColor.getColor("#09F"));
		System.out.println( HTMLColor.getColor("#09F"));
		System.out.println( HTMLColor.getColor("#09F"));

		System.out.println( HTMLColor.getColor("rgb(50,61,72)"));
		System.out.println( HTMLColor.getColor("rgb(50,61,72,0)"));
		System.out.println( HTMLColor.getColor("rgb(50,61,72,1)"));
		System.out.println( HTMLColor.getColor("rgba(0,0,0,0.3)"));

	}
}
